package com.foreverwin.mes.common.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreverwin.mes.core.exception.BusinessException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ervin Chen
 * @date 2020/3/6 11:45
 */
public class PrintRequest<T> {

    private String printer;
    private int labelQty;
    private String template;
    private List<T> printData = new ArrayList<>();

    public String getPrinter() {
        return printer;
    }

    public void setPrinter(String printer) {
        this.printer = printer;
    }

    public int getLabelQty() {
        return labelQty;
    }

    public void setLabelQty(int labelQty) {
        this.labelQty = labelQty;
    }

    public String getTemplate() {
        return template;
    }

    public void setTemplate(String template) {
        this.template = template;
    }

    public List<T> getPrintData() {
        return printData;
    }

    public boolean addPrintData(T printParam){
        return this.printData.add(printParam);
    }

    @Override
    public String toString() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return "{  \"PRINT_REQUEST\":{" +
                    "  \"PRINT_NAME\":\"" + printer + "\"" +
                    ", \"LABEL_QTY\":" + labelQty +
                    ", \"TEMPLATE\":\"" + template + "\"" +
                    ", \"PRINT_DATA\":" + objectMapper.writeValueAsString(printData) +
                    "  }  }";
        } catch (JsonProcessingException e) {
            throw BusinessException.build( e.getMessage() );
        }
    }

    public static void main(String[] args){
        PrintRequest<Map<String,Object>> printRequest = new PrintRequest<>();
        Map<String,Object> printMap = new HashMap<>();
        printMap.put( "INVENTORY_ID", "201910120001" );
        printMap.put( "ITEM_NO", "0101010001" );
        printMap.put( "ITEM_NAME", "哥伦比亚蕙兰咖啡生豆" );
        printMap.put( "QTY", "60" );
        printMap.put( "UNIT", "KG" );
        printMap.put( "INV_BATCH", "20190919" );
        printMap.put( "EXP_DATE", "2020-09-19" );

        printRequest.setPrinter("XPS");
        printRequest.setLabelQty(1);
        printRequest.setTemplate("doc");
        printRequest.addPrintData(printMap);
        System.out.println(printRequest);
    }
}
